package com.dstz.bus.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dstz.base.api.exception.BusinessError;
import com.dstz.base.api.response.impl.ResultMsg;
import com.dstz.base.core.util.RequestContext;
import com.dstz.base.core.util.RestTemplateUtil;
import com.dstz.bus.api.constant.BusStatusCode;
import com.dstz.bus.api.constant.BusinessObjectPersistenceType;
import com.dstz.bus.api.remote.RemoteBusinessData;
import com.dstz.bus.model.BusTableRel;
import com.dstz.bus.model.BusinessData;
import com.dstz.bus.model.BusinessObject;
import com.dstz.bus.service.BusinessDataPersistenceService;
import com.dstz.bus.service.BusinessDataService;

/**
 * <pre>
 * 描述：http远程调用方式保存
 * 作者:aschs
 * 邮箱:aschs@qq.com
 * 日期:2019年7月5日
 * 版权:summer
 * </pre>
 */
@Service
public class BusinessDataPersistenceHttpService implements BusinessDataPersistenceService {
	@Autowired
	protected BusinessDataService businessDataService;

	@Override
	public String type() {
		return BusinessObjectPersistenceType.HTTP.getKey();
	}

	@Override
	public void saveData(BusinessData businessData) {
		BusinessObject businessObject = businessData.getBusTableRel().getBusObj();
		// 数据
		RemoteBusinessData<JSONObject> remoteBusinessData = new RemoteBusinessData<>();
		JSONObject data = businessDataService.assemblyFormDefData(businessData);
		remoteBusinessData.setData(data);
		remoteBusinessData.setBoKey(businessObject.getKey());

		// url
		JSONObject config = JSON.parseObject(businessObject.getPerTypeConfig());
		String url = RequestContext.getUrl(config.getString("saveDataUrl"));

		// json 发 http 到url中
		ResultMsg<String> result = RestTemplateUtil.post(url, remoteBusinessData, ResultMsg.class);
		if (!result.getIsOk()) {
			throw new BusinessError("saveDataUrl请求返回isOk为false", BusStatusCode.PERSISTENCE_HTTP_ERR);
		}
		businessData.setPk(result.getData());
	}

	@Override
	public BusinessData loadData(BusinessObject businessObject, Object id) {
		BusinessData businessData = new BusinessData();
		// 处理主表
		BusTableRel busTableRel = businessObject.getRelation();
		businessData.setBusTableRel(busTableRel);
		if (id == null) {// 主键为空
			return businessData;
		}

		// url
		JSONObject config = JSON.parseObject(businessObject.getPerTypeConfig());
		String url = RequestContext.getUrl(config.getString("loadDataUrl"));

		// json 发 http 到url中
		RemoteBusinessData remoteBusinessData = new RemoteBusinessData<>();
		remoteBusinessData.setBoKey(businessObject.getKey());
		remoteBusinessData.setId(id);

		JSONObject result = RestTemplateUtil.post(url, remoteBusinessData, JSONObject.class);
		if (!result.getBooleanValue("isOk")) {
			throw new BusinessError("loadDataUrl请求返回isOk为false", BusStatusCode.PERSISTENCE_HTTP_ERR);
		}
		JSONObject data = result.getJSONObject("data").getJSONObject("data");
		businessData = (BusinessData) businessDataService.parseBusinessData(data, businessObject.getKey());

		return businessData;
	}

	@Override
	public void removeData(BusinessObject businessObject, Object id) {
		// url
		JSONObject config = JSON.parseObject(businessObject.getPerTypeConfig());
		String url = RequestContext.getUrl(config.getString("removeDataUrl"));

		// json 发 http 到url中
		RemoteBusinessData remoteBusinessData = new RemoteBusinessData<>();
		remoteBusinessData.setBoKey(businessObject.getKey());
		remoteBusinessData.setId(id);

		JSONObject result = RestTemplateUtil.post(url, remoteBusinessData, JSONObject.class);
		if (!result.getBooleanValue("isOk")) {
			throw new BusinessError("removeDataUrl请求返回isOk为false", BusStatusCode.PERSISTENCE_HTTP_ERR);
		}
	}

	public static void main(String[] args) {
		JSONObject json = JSON.parseObject("{\"code\":\"200\",\"isOk\":true,\"page\":1,\"pageSize\":10,\"rows\":[{\"a\":\"1\",\"id\":409729659240185857}],\"total\":1}");
		System.out.println(json.toJSONString());
	}
}
